<?php

class mysql{
   
  function link($ipstr="",$database="",$username="",$password="",$port)
  {
      if($database==""){
          $this->database="dyhekou";
      }else{
          $this->database=$database;
      }
	  if (trim($ipstr)==""){$port="localhost";}
	  if (trim($port)!=""){$port=":".$port;}
	
      if($conn=mysql_connect($ipstr.$port,$username,$password)){
		if(!mysql_select_db($this->database,$conn)){
		echo "数据库连接错误！！！";
		exit;
		}
	  }else{
		echo"服务器正在维护中……";
		exit;
	  }
  }


  function excu($query)
  {//执行sql语句
      mysql_query("set Names utf8");
      if($result=@mysql_query($query)){
          return $result;
      }else{
          echo "  sql语句执行错误！ 请重试！";
          if (DEBUG_MODE){echo "<BR>".mysql_error()."<BR>";}
          exit;
      }
  }

  function fetch_array($query, $result_type = MYSQL_ASSOC) {
      return mysql_fetch_array($query, $result_type);
  }

  function num_rows($query) {
      $rows = mysql_num_rows($query);
      return $rows;
  }

  function free_result($query) {
      return mysql_free_result($query);
  }


  function get_one($SQL){
      $query=$this->excu($SQL,'U_B');
      $rs =& mysql_fetch_array($query, MYSQL_ASSOC);
      return $rs;
  }
  //获得一行数据
  function getinfo($sql){
      return @$this->fetch_array($this->excu($sql));
  }
  //获得单个字段
  function getsingle($sql){
      $result=$this->excu($sql);
      $i=mysql_num_rows($result);
      if($i==0){return "";}
      else{
          $res=mysql_fetch_array($result);
          return $res[0];
      }
  }
  //获得数据数量
  function listcount($table,$str){
      $sql="select count(*) as c from $table where 0=0 $str";
//	  echo $sql;
//	  exit();
      return mysql_result($this->excu($sql),0,"c");
  }

  //分页
  function list1($pageindex,$pagesize,$table,$column,$str,$order,$biaoid=""){
      if(trim($order)==""){$biaoid="id";}
      if($order==""){$order=" order by {$biaoid} desc";}
      $offset=$pagesize*($pageindex - 1);
      $sql="select $column from $table where 0=0 $str $order  limit $offset,$pagesize";
//	  echo $sql;
//	  exit();
      $result=mysql_query($sql) or die($sql) ;
      $ar=array();
      while($re=mysql_fetch_array($result)){
            array_unshift($ar,$re);
      }
      return array_reverse($ar);
  }

  function ThePage($Pid,$page=1,$Vcount,$Pnum,$className="",$linkclass="",$showinfo=0,$pagelen=4,$aname=""){
      $Pcount = ceil($Vcount/$Pnum);
      if($page>$Pcount) $page=$Pcount;
	  $page_up=$page-1;
	  $page_down=$page+1;
      $show='<div class="black2">';
	  if ($page==1){
      $show.='<span class="disabled"> &lt;&lt; </span>';
	  }
	  else{
      $show.='<a href="?'.$Pid.'pageindex=1'.$aname.'" title="首页" />&lt;&lt;</a>';
	  }
      if($page>1){
          $show.='<a href="?'.$Pid.'pageindex='.$page_up.$aname.'" title="上一页" /> &lt; </a>';
      }else{
	  	$show.='<span class="disabled">&lt;</span>';
	  }
	  $init=1; 
	  $max = $Pcount;//结束页码数 
      $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数 
      $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量 
      
	  if($Pcount>$pagelen){ 
          //如果当前页小于等于左偏移 
          if($page<=$pageoffset){ 
              $init=1; 
              $max = $pagelen; 
          }else{//如果当前页大于左偏移 
          //如果当前页码右偏移超出最大分页数 
              if($page+$pageoffset>=$Pcount){ 
                  $init = $Pcount-$pagelen+1; 
              }else{ 
              //左右偏移都存在时的计算 
                  $init = $page-$pageoffset; 
                  $max = $page+$pageoffset; 
              } 
          } 
      }
      //生成html 
      for($i=$init;$i<=$max;$i++){ 
          if($i<=0){$i=1;}
          if($i<=$Pcount){
              $fenye_show=($i!=$page)?"<a href=?".$Pid."pageindex=".$i.$aname.">$i</a>":"<span class=\"current\">$i</span>";
			  $show.=$fenye_show;
		  }
      } 
	  
      if($page<$Pcount){	
          $show.='<a href="?'.$Pid.'pageindex='.$page_down.$aname.'" title="下一页" />&gt;</a>';			
      }
	  else{$show.='<span class="disabled">&gt;</span>';}
	  if($page==$Pcount){$show.='<span class="disabled">&gt;&gt;</span>';}
	  else{		
      $show.='<a href="?'.$Pid.'pageindex='.$Pcount.$aname.'" title="尾页" />&gt;&gt;</a>';
	  }
	  if ($showinfo==1){
      $show.="<span style=\"float:right;margin-left:5px;\">页</span><span class=\"goto_input\"><input type=\"text\" name=\"pageindex\" size=\"3\" onkeyup=\"if(event.keyCode == 13){location.href='?{$Pid}pageindex='+this.value;}\"></span><span style=\"float:right;margin-left:10px;\">转</span>";
      $show.='<span style="float:right;">&nbsp;&nbsp;第&nbsp;<font color="red">'.$page.'</font>&nbsp;页&nbsp;&nbsp;&nbsp;<font color="red">'.$Pnum.'</font>条/页&nbsp;&nbsp;&nbsp;共&nbsp;<font color="red">'.$Pcount.'</font>&nbsp;页&nbsp;<font color="red">'.$Vcount.'</font>&nbsp;条</span>';
	  }

      $show.="</div>";
      return $show;
  }
  
  function ThePage_js($Pid,$page=1,$Vcount,$Pnum,$className="black2",$set_page="set_page",$showinfo=0,$pagelen=7){
      $Pcount = ceil($Vcount/$Pnum);
      if($page>$Pcount) $page=$Pcount;
	  $page_up=$page-1;
	  $page_down=$page+1;
      $show='<div class="'.$className.'">';
	  if ($page==1){
      $show.='<span class="disabled"> &lt;&lt; </span>';
	  }
	  else{
      $show.="<a href=\"javascript:$set_page('1');\" title=\"首页\" >&lt;&lt;</a>";
	  }
      if($page>1){
          $show.="<a href=\"javascript:$set_page('{$page_up}');\" title=\"上一页\" > &lt; </a>";
      }else{
	  	$show.='<span class="disabled">&lt;</span>';
	  }
	  $init=1; 
	  $max = $Pcount;//结束页码数 
      $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数 
      $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量 
      
	  if($Pcount>$pagelen){ 
          //如果当前页小于等于左偏移 
          if($page<=$pageoffset){ 
              $init=1; 
              $max = $pagelen; 
          }else{//如果当前页大于左偏移 
          //如果当前页码右偏移超出最大分页数 
              if($page+$pageoffset>=$Pcount){ 
                  $init = $Pcount-$pagelen+1; 
              }else{ 
              //左右偏移都存在时的计算 
                  $init = $page-$pageoffset; 
                  $max = $page+$pageoffset; 
              } 
          } 
      }
      //生成html 
      for($i=$init;$i<=$max;$i++){ 
          if($i<=0){$i=1;}
          if($i<=$Pcount){
              $fenye_show=($i!=$page)?"<a href=\"javascript:$set_page('{$i}');\">{$i}</a>":"<span class=\"current\">$i</span>";
			  $show.=$fenye_show;
		  }
      } 
	  
      if($page<$Pcount){	
          $show.="<a href=\"javascript:$set_page('{$page_down}');\" title=\"下一页\" >&gt;</a>";			
      }
	  else{$show.='<span class="disabled">&gt;</span>';}
	  if($page==$Pcount){$show.='<span class="disabled">&gt;&gt;</span>';}
	  else{		
      $show.="<a href=\"javascript:$set_page('{$Pcount}');\" title=\"尾页\" >&gt;&gt;</a>";
	  }

      $show.="</div>";
      return $show;
  }

  function ThePage_mxsoft($Pid,$page=1,$Vcount,$Pnum,$className="black2",$linkclass="",$showinfo=0,$pagelen=10,$aname=""){
  	  if (trim($className)==""){$className="black2";}
      $Pcount = ceil($Vcount/$Pnum);
      if($page>$Pcount) $page=$Pcount;
	  $page_up=$page-1;
	  $page_down=$page+1;
      $show='<div class="'.$className.'">';
	  if ($page==1){
      $show.='<span class="disabled"> &lt;&lt; </span>';
	  }
	  else{
      $show.='<a href="?'.$Pid.'pageindex=1'.$aname.'" title="首页" />&lt;&lt;</a>';
	  }
      if($page>1){
          $show.='<a href="?'.$Pid.'pageindex='.$page_up.$aname.'" title="上一页" /> &lt; </a>';
      }else{
	  	$show.='<span class="disabled">&lt;</span>';
	  }
	  $init=1; 
	  $max = $Pcount;//结束页码数 
      $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数 
      $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量 
      
	  if($Pcount>$pagelen){ 
          //如果当前页小于等于左偏移 
          if($page<=$pageoffset){ 
              $init=1; 
              $max = $pagelen; 
          }else{//如果当前页大于左偏移 
          //如果当前页码右偏移超出最大分页数 
              if($page+$pageoffset>=$Pcount){ 
                  $init = $Pcount-$pagelen+1; 
              }else{ 
              //左右偏移都存在时的计算 
                  $init = $page-$pageoffset; 
                  $max = $page+$pageoffset; 
              } 
          } 
      }
      //生成html 
      for($i=$init;$i<=$max;$i++){ 
          if($i<=0){$i=1;}
          if($i<=$Pcount){
              $fenye_show=($i!=$page)?"<a href=?".$Pid."pageindex=".$i.$aname.">$i</a>":"<span class=\"current\">$i</span>";
			  $show.=$fenye_show;
		  }
      } 
	  
      if($page<$Pcount){	
          $show.='<a href="?'.$Pid.'pageindex='.$page_down.$aname.'" title="下一页" />&gt;</a>';			
      }
	  else{$show.='<span class="disabled">&gt;</span>';}
	  if($page==$Pcount){$show.='<span class="disabled">&gt;&gt;</span>';}
	  else{		
      $show.='<a href="?'.$Pid.'pageindex='.$Pcount.$aname.'" title="尾页" />&gt;&gt;</a>';
	  }
	  if ($showinfo==1){
      $show.="<span style=\"float:right;margin-left:5px;\">页</span><span class=\"goto_input\"><input type=\"text\" name=\"pageindex\" size=\"3\" onkeyup=\"if(event.keyCode == 13){location.href='?{$Pid}pageindex='+this.value;}\"></span><span style=\"float:right;margin-left:10px;\">转</span>";
      $show.='<span style="float:right;">&nbsp;&nbsp;第&nbsp;<font color="red">'.$page.'</font>&nbsp;页&nbsp;&nbsp;&nbsp;<font color="red">'.$Pnum.'</font>条/页&nbsp;&nbsp;&nbsp;共&nbsp;<font color="red">'.$Pcount.'</font>&nbsp;页&nbsp;<font color="red">'.$Vcount.'</font>&nbsp;条</span>';
	  }

      $show.="</div>";
      return $show;
  }

  //单引号转换
  function mysql_string($str){
      if ($str!=""){$str=stripslashes($str);}
      if ($str!=""){$str=str_replace("'","''",$str);}
	  return $str;
  }

 	/**
	 * 生成INSERT语句
	 * @param	String		$table			表名
	 * @param	Array		$insert_fields	数据数组
	 * @return	String	
	 * @access	private
	 */  
	function sql_insert($table, $insert_fields)
	{
		$fields = "";
		$values = "";

		if(gettype($insert_fields) != "array")
		{ 
			$this->halt("方法\"sql_insert()\"的第二项参数的类型应为数组！");
		}
		else
		{
			while(list($key,$val) = each($insert_fields))
			{
				$fields .= trim($key) . ", ";
				$values .= $this->checkType($val) .", ";
			}

			$fields = substr($fields,0,strlen($fields) - 2);
			$values = substr($values,0,strlen($values) - 2);
			$sql = sprintf( "insert into %s (%s) values (%s)", strtolower( $table ), strtolower( $fields ), $values);
		}
				  
		return $sql;
	} 

	/**
	 * 生成UPDATE语句
	 * @param	String		$table			表名
	 * @param	Array		$update_fields	数据数组
	 * @param	String		$where			"WHERE"子句
	 * @return	String	
	 * @access	private
	 */
	function sql_update($table, $update_fields, $where = "")
	{
		if(gettype($update_fields) != "array")
		{ 
			$this->halt("方法\"sql_update()\"的第二项参数的类型应为数组！");
		}
		else
		{
			$fields = "";
			$values = "";
		   
			while(list($key,$val) = each($update_fields))
			{            
				if(gettype($val) == "array")
				{ 
					$value	  = $this->checkType(trim($val[0]));
					$operator = trim($val[1]);
				}
				else
				{
					$value	  = $this->checkType(trim($val)); 
					$operator = "";
				}
			  
				switch($operator)
				{
					case "+" :
						$value = "{$key} + {$value}, ";
						break;
					case "-" :
						$value = "{$key} - {$value}, ";
						break;
					case "*" :
						$value = "{$key} * {$value}, ";
						break;
					case "/" :
						$value = "{$key} / {$value}, ";
						break;                   
					default:
						$value = "{$value}, ";
				}
				$fields .= trim($key) . " = " . $value ;
			}      

			$fields = substr($fields,0,strlen($fields) - 2); 
	 
			if(trim($where) != "")
			{           
				$sql = sprintf( "update %s set %s where %s", strtolower( $table ), $fields, $where );
				return $sql;
			}
			else
			{
				$sql = sprintf( "update %s set %s ", strtolower( $table ), $fields );
			}   
		}
		return $sql;
	}
   
	/**
	 * 生成DELETE语句
	 * @param	String		$table		表名
	 * @param	String		$where		"WHERE"子句
	 * @return	String	
	 * @access	private
	 */	
	function sql_delete($table, $where = "")
	{
		if(trim($where) != "")
		{
			$where_clause = " WHERE ".$where." ";
		}
		else
		{
			$where_clause = "";
		}
				  
		$sql = "delete from ".$table." {$where_clause}";
		$sql = strtolower( $sql );
		return $sql;
	}
	/**
	 * 执行INSERT语句
	 * @param	String		$table			表名
	 * @param	Array		$insert_fields	数据数组
	 * @param	int			$log			是否将本次数据库操作记录到日志中  0：否   1：是
	 * @return	Integer	
	 * @access	public
	 */	
	function exe_insert($table, $insert_fields, $log = 0)
	{
		$sql	= $this->sql_insert($table, $insert_fields);
//		echo $sql;
//		exit();
		$query	= $this->excu($sql);
		if ($log == 1){$this->dblog('Insert', $table, $sql);}
		return $query;
	} 

	/**
	 * 执行UPDATE语句
	 * @param	String		$table			表名
	 * @param	Array		$update_fields	数据数组
	 * @param	String		$where			"WHERE"子句
	 * @param	int			$log			是否将本次数据库操作记录到日志中  0：否   1：是
	 * @return	Integer	
	 * @access	public
	 */	
	function exe_update($table, $update_fields, $where = "", $log = 0)
	{
		$sql	= $this->sql_update($table, $update_fields, $where);
//		echo $sql;
//		exit();
		$query	= $this->excu($sql);
		if ($log == 1){$this->dblog('Update', $table, $sql);}
		return $query;
	} 

	/**
	 * 执行DELETE语句
	 * @param	String		$table		表名
	 * @param	String		$where		"WHERE"子句
	 * @param	int			$log		是否将本次数据库操作记录到日志中  0：否   1：是
	 * @return	Integer	
	 * @access	public
	 */	
	function exe_delete($table, $where = "", $log = 0)
	{
		$sql	= $this->sql_delete($table, $where);
		$query	= $this->excu($sql);
		if ($log == 1){$this->dblog('Delete', $table, $sql);}
		return $query;
	}
	/**
	 * 将数据库操作记录到日志库中
	 * @param	string	$type		操作类型
	 * @param	string	$table		操作对象：表名
	 * @param	string	$sql		操作内容：SQL语句
	 * @return	null	
	 * @access	public
	 */
	function dblog($type, $table, $sql)
	{
		global $HUB;
		if($_SESSION['sys']['empid'] == "")
		{
			$time = date("Y-m-d H:i:s");
			$ip = getenv("REMOTE_ADDR");
			$field = array(
			'fid'			=> $_SESSION['sys']['fid'],
			'userid'		=> $_SESSION['sys']['userid'],
			'username'		=> $_SESSION['sys']['username'],
			'userip'		=> $ip,
			'operatetype'	=> $type,
			'operateobject' => $table,
			'operatecontent'=> str_replace("'","''", $sql),
			'operatetime'	=> $time,
			);
			$this->exe_insert("jl_dblog", $field, 0);
		}	
	}
	/**
	 * 根据给定变量的标记转换变量类型
	 * @param	String		$var	标记过的变量
	 * @return	String				转换后的变量
	 * @access	private
	 */
	function checkType($var)
	{
		if (substr($var,0,5) == "[int]" and substr($var,-6) == "[/int]") 
		{
			$var = substr($var,5,-6);
			return $var;
		}
		if ( substr( $value, 0, 6 ) == "[date]" && substr( $value, -7 ) == "[/date]" )
		{
			$var = trim( substr( $value, 6, -7 ) );
			if ( $var == "" )
			{
				$var = "'0000-00-00'";
				return $var;
			}
			$var = "'".$var."'";
			return $var;
		}
		$var = "'".$this->mysql_string($var)."'";
		return $var;
	}
	/**
	 * 终止程序运行并显示错误信息
	 * @access	private
	 */   
	function halt($msg="操作语句错误")
	{ 
	    echo "<script>alert('{$msg}');history.back();</script>";
		exit();
	}
  

  //获得下一级别分类
  function get_aid($fup,$table="mx_vpp_articleclass"){
      $sql="select fid,sons from $table  where fup in ($fup)";
      $rs=$this->excu($sql);
      if($this->num_rows($rs)>0){
          while($rsdb=$this->fetch_array($rs)){
	          $fupid.=",$rsdb[fid]";
		      if ($rsdb["sons"]>0){
		          $fupid.=$this->get_aid($rsdb[fid],$table);
		      }
	      }
      }
      else{$fupid="";}
      return $fupid;
  }
  
    //获得文章位置
  function get_pro_dao($fid,$table="mx_vpp_articleclass",$path="/shop/"){
  	  global $db;
      $sql="select * from $table where fid='$fid'";
      $rs=$this->excu($sql);
      if($this->num_rows($rs)>0){
          while($rsdb=$this->fetch_array($rs)){
		  	  if ($rsdb["sons"]>0 and $rsdb["fup"]!=0){$fupid="$rsdb[name]|".$fupid;}
			  elseif ($rsdb["fup"]==0){
				$fupid="$rsdb[name]|".$fupid;
			  }
			  else{$fupid="$rsdb[name]|".$fupid;}
		      if ($rsdb["fup"]!=0){
		          $fupid=$this->get_pro_dao($rsdb["fup"],$table,$path).$fupid;
		      }
	      }
      }
      else{$fupid="";}
      return $fupid;
  }
	
  function get_pro_classcode($fid,$table="mx_vpp_articleclass"){
      $sql="select * from $table where fid='$fid'";
      $rs=$this->excu($sql);
      if($this->num_rows($rs)>0){
          while($rsdb=$this->fetch_array($rs)){
		  	  if ($rsdb["sons"]>0 and $rsdb["fup"]!=0){$fupid="$rsdb[classcode]".$fupid;}
			  elseif ($rsdb["fup"]==0){
				$fupid="$rsdb[classcode]".$fupid;
			  }
			  else{$fupid="$rsdb[classcode]".$fupid;}
		      if ($rsdb["fup"]!=0){
		          $fupid=$this->get_pro_classcode($rsdb["fup"],$table).$fupid;
		      }
	      }
      }
      else{$fupid="";}
      return $fupid;
  }
  
  //获得操作用户名称
  function getmembername($uid){
  	$uname="";
  	$userinfo=$this->getinfo("select * from mx_members where uid='$uid'");
  	if (trim($userinfo["realname"])==""){$userinfo["realname"]=$userinfo["username"];}
	return $userinfo;
  }


  function getuserinfo($userid,$cwpass=""){
  	  $userinfo="";
  	  $userinfo=$this->getinfo("select * from mx_user where id='$userid' limit 1");
  	  if (trim($cwpass)!=""){
	  	if (trim($userinfo["cwpassword"])==""){
			return "-1";
		}
		if (trim($userinfo["cwpassword"])!=md5($cwpass) and $cwpass!=-1){
			$userinfo="";
		}
	  }
	  if (is_array($userinfo)){
	  	  if (trim($userinfo["nicheng"])==""){$userinfo["nicheng"]=$userinfo["weixinid"];}
		  if (trim($userinfo["realname"])==""){$userinfo["realname"]=$userinfo["nicheng"];}
		  $userinfo["weburl"]="/user/user_show.php?id=$userinfo[id]";
		  $userinfo["weburl"]="#";
	  }
	  return $userinfo;
  }
  function search_user($keywords){
  	$useridstr="";
  	$rs=$this->excu("select * from mx_user where id>'0' $usersql");
	if ($this->num_rows($rs)>0){
		while ($rsdb=$this->fetch_array($rs)){
			if ($useridstr==""){$useridstr=$rsdb["id"];}
			else{$useridstr.=",".$rsdb["id"];}
		}
	}
	if ($useridstr==""){$useridstr=-1;}
	return $useridstr;
  }
  //获得第一级别分类
  function get_f_aid($fid,$table="mx_vpp_articleclass"){
      $sql="select fid,fup,class from $table where fid='$fid'";
      $rs=$this->excu($sql);
      if($this->num_rows($rs)>0){
          while($rsdb=$this->fetch_array($rs)){
		      if ($rsdb["class"]==1 or $rsdb["fup"]==0){$fupid=$rsdb["fid"];}
			  else{
		          $fupid=$this->get_f_aid($rsdb["fup"],$table);
		      }
	      }
      }
      else{$fupid="";}
      return $fupid;
  }
  //获取产品货号
  function get_pro_code($fid){
  	if ($fid>0){
		$nowinfo=$this->getinfo("select * from mx_pro_code where thisdate='".date("Y-m-d")."' limit 1");
		if (is_array($nowinfo)){
			$maxid=$nowinfo["maxid"]+1;
			$this->excu("update mx_pro_code set maxid='$maxid' where thisdate='".date("Y-m-d")."'");
		}
		else{
			$maxid=1;
			$this->excu("update mx_pro_code set maxid='$maxid',thisdate='".date("Y-m-d")."'");
		}
		$classcode=$this->get_pro_classcode($fid);
		$procode1=date("ymd").sprintf("%05d", $maxid);
		$procode=$classcode.$procode1;
		$procodearr=array($procode,$procode1);
	}
	else{
		$procodearr="";
	}
	return $procodearr;
  }
  //获取数据
  function get_pro($fid=0,$wheresql="",$order="",$getnum=10){
  	$fid=intval($fid);
	if ($fid>0){
		$idstr=$fid.$this->get_aid($fid);
		$wheresql.=" and fid in ($idstr) ";
	}
	
	if (trim($order)==""){
		$order=" isshou asc,isgood desc,istop desc, list asc, posttime desc, aid desc";
	}
	$sql="select * from mx_pro where yz='1' and (ismiao='0' or ismiao='' or ismiao is null)  $wheresql order by $order ";

	if ($getnum>0){
		$sql.=" limit $getnum";
	}
//	echo $sql;
//	exit();
	$row="";
	$rs=$this->excu($sql);
	if ($this->num_rows($rs)>0){
		while ($rsdb=$this->fetch_array($rs)){
		if ($rsdb["ismianyi"]==1){$rsdb["price"]="议价";}
		$row[]=$rsdb;
		}
	}
	return $row;
  }
  //获取数据信息
  function get_article($fid=0,$wheresql="",$order="",$getnum=10){
  	$fid=intval($fid);
	$getnum=intval($getnum);
	if ($fid>0){
		$idstr=$fid.$this->get_aid($fid,"mx_vpp_articleclass");
		$wheresql.=" and fid in ($idstr) ";
	}
	if (trim($order)==""){
		$order=" isgood desc,istop desc, orderid, posttime desc, aid desc";
	}
	$sql="select * from mx_vpp_article where yz='1'  $wheresql order by $order";
	if ($getnum>0){
		$sql.=" limit $getnum";
	}
	$row="";
	$rs=$this->excu($sql);
	if ($this->num_rows($rs)>0){
		while ($rsdb=$this->fetch_array($rs)){
		if (trim($rsdb["smalltitle"])==""){$rsdb["smalltitle"]=$rsdb["title"];}
		$row[]=$rsdb;
		}
	}
	return $row;
  }

  //获取数据
  function get_danye($id=0){
	$sql="select * from mx_vpp_infor where id='$id' limit 1";
	$row="";
	$rsdb=$this->getinfo($sql);
	if (is_array($rsdb)>0){
			$rsdb["content"]=stripslashes($rsdb["content"]);
			$row=$rsdb;
	}
	return $row;
  }
  
    //更新关键字列表
  function update_searchkey($keywords,$stype,$isupdate=0){
  	if (($keywords!="" and $stype!="" and !strpos("|||".$_SESSION["wbysw"][$stype]["keywords"],"|$keywords|")) or $isupdate==1){
		$haveinfo=$this->getinfo("select * from mx_searchkey where keywords='$keywords' and stype='$stype' limit 1");
		if (is_array($haveinfo)){
			$this->excu("update mx_searchkey set hits=hits+1 where  keywords='$keywords' and stype='$stype'");
		}
		else{
			$this->excu("insert into mx_searchkey(keywords,hits,stype) values('$keywords','1','$stype')");
		}
		if ($_SESSION["wbysw"][$stype]["keywords"]==""){
			$_SESSION["wbysw"][$stype]["keywords"]="|$keywords|";
		}
		else{
			$_SESSION["wbysw"][$stype]["keywords"].="$keywords|";
		}
	}
  }
  //获取热门关键词
  function show_hotkeyword($stype="",$getnum=0){
  	$row="";
	$wheresql="";
	$getnumsql="";
	if ($stype!=""){$wheresql.=" and stype='$stype' ";}
	$getnum=intval($getnum);
	if ($getnum>0){$getnumsql.=" top $getnum ";}
  	$rs=$this->excu("select $getnumsql * from mx_searchkey where 1=1 $wheresql order by hits desc,id desc");
	if ($this->num_rows($rs)>0){
		while ($rsdb=$this->fetch_array($rs)){
			if ($rsdb["stype"]=="picture"){
				$rsdb["weburl"]="/zhanxun/search.php?keyword=".urlencode($rsdb["keywords"]);
			}
			else if ($rsdb["stype"]=="video"){
				$rsdb["weburl"]="/video/search.php?keyword=".urlencode($rsdb["keywords"]);
			}
			else if ($rsdb["stype"]=="shop"){
				$rsdb["weburl"]="/shop/search.php?keyword=".urlencode($rsdb["keywords"]);
			}
			else{
				$rsdb["weburl"]="/search.php?keyword=".urlencode($rsdb["keywords"]);
			}
			$row[]=$rsdb;
		}
	}
	return $row;
  }
  //更新标签列表
  function update_tags($keywords,$stype,$isupdate=0){
  	if (($keywords!="" and $stype!="" and !strpos("|||".$_SESSION["tsw"][$stype]["tags"],"|$keywords|") ) or $isupdate==1){
		$haveinfo=$this->getinfo("select * from mx_tags where keywords='$keywords' and stype='$stype' limit 1");
		if (is_array($haveinfo)){
			$this->excu("update mx_tags set hits=hits+1 where  keywords='$keywords' and stype='$stype'");
		}
		else{
			$this->excu("insert into mx_tags(keywords,hits,stype) values('$keywords','1','$stype')");
		}
		if ($_SESSION["tsw"][$stype]["tags"]==""){
			$_SESSION["tsw"][$stype]["tags"]="|$keywords|";
		}
		else{
			$_SESSION["tsw"][$stype]["tags"].="$keywords|";
		}
	}
  }
  //获取热门关键词
  function show_webtags($fid="",$getnum=0,$order="",$gotourl="/search.php?keyword="){
  	$row="";
	$wheresql=" and isok='1' ";
	$getnumsql="";
	$fid=intval($fid);
	if ($fid!=0){$wheresql.=" and fid='$fid' ";}
	$getnum=intval($getnum);
	if ($getnum>0){$getnumsql.=" top $getnum ";}
	if (trim($order)==""){$order=" orderid asc,id desc";}
	if (trim($gotourl)==""){$gotourl="/search.php?";}
  	$rs=$this->excu("select $getnumsql * from mx_web_tags where 1=1 $wheresql order by $order ");
	if ($this->num_rows($rs)>0){
		while ($rsdb=$this->fetch_array($rs)){
			$rsdb["gotourl"]=$gotourl.urlencode($rsdb["tagsname"]);
			$row[]=$rsdb;
		}
	}
	return $row;
  }
  
  //获取最后插入的id
  function getInsertId(){
  	return mysql_insert_id();
  }
}
?>